package com.adinnet.admin.bootPage.controller;


import com.adinnet.admin.bootPage.model.BootPage;
import com.adinnet.admin.bootPage.service.BootPageService;
import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.dict.model.Dict;
import com.adinnet.admin.dict.service.DictService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageParam;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 启动页表 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2021-03-03
 */
@Controller
@RequestMapping("admin/bootPage")
public class BootPageController {

    @Autowired
    private BootPageService bootPageService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private DictService dictService;

    @RequestMapping("")
    public String bootPage(Model model) {
        Integer isNew=0;
        Integer isShowBootPage = 0;
        Dict dict = dictService.getOne(new QueryWrapper<Dict>().eq("dict_code", "BOOT_PAGE"));
        BootPage bootPage = bootPageService.getOne(new QueryWrapper<BootPage>().last("limit 1"));
        if(bootPage!=null){
            isNew=1;
        }
        if(null!=dict){
            isShowBootPage = dict.getSortNum();
        }
        model.addAttribute("isNew",isNew);
        model.addAttribute("isShowBootPage",isShowBootPage);
        return "bootPage/bootPage.html";
    }

    /**
     * 查询启动页列表
     **/
    @ResponseBody
    @RequestMapping("list")
    public PageResult<BootPage> list(PageParam req) {
        return bootPageService.bootPageList(req);
    }

    /**
     * 新增启动页
     **/
    @ResponseBody
    @RequestMapping("add")
    public JsonResult add(BootPage bp, HttpServletRequest request) {
        if (bootPageService.save(bp)) {
            commonService.saveOperation(request,"启动页管理-新增启动页,启动页id为："+bp.getId());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    /**
     * 修改广告
     **/
    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(BootPage ad,HttpServletRequest request) {
        if (bootPageService.updateById(ad)) {
            commonService.saveOperation(request,"启动页管理-编辑启动页,启动页id为："+ad.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }
}
